package net.ycxinxing.mcpebox.ui;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import net.ycxinxing.mcpebox.Pro.R;
import net.ycxinxing.mcpebox.baseActivity;
import net.ycxinxing.mcpebox.bean.mUser;
import net.ycxinxing.mcpebox.mDialog;
import android.widget.TextView;

public class AccountActivity extends baseActivity
{
	mUser user;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		user=BmobUser.getCurrentUser(mUser.class);
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		if(user==null)
		{
			mDialog dlg=new mDialog(this){
				@Override
				public void onButtonClick(int i)
				{
					AccountActivity.this.finish();
					// TODO: Implement this method
				}
			};
			dlg.setMsg("您尚未登陆");
			dlg.setCanClose(false);
			dlg.setButton3("确定");
			dlg.show();
			return;
		}
		LinearLayout l=new LinearLayout(this);
		l.setOrientation(1);
		View info=LayoutInflater.from(this).inflate(R.layout.view_account,null);
		ImageView image=(ImageView)info.findViewById(R.id.viewaccountImageView1);
		((TextView)info.findViewById(R.id.viewaccountTextView1)).setText(user.getUsername());
		((TextView)info.findViewById(R.id.viewaccountTextView2)).setText("个性签名"+user.getSign());
		if(user.getAvatar()==null)
		{
			image.setImageResource(R.drawable.user);
		}else{
			ImageLoader.getInstance().displayImage(user.getAvatar().getUrl(),image);
		}
		Button logout=new Button(this);
		logout.setText("登出");
		//l.addView(info);
		l.addView(logout);
		logout.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					user.logOut();
					finish();
					// TODO: Implement this method
				}
			});
		Button xgmm=new Button(this);
		xgmm.setText("修改密码");
		xgmm.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					LinearLayoutCompat dlgl=new LinearLayoutCompat(AccountActivity.this);
					dlgl.setOrientation(1);
					TextInputLayout LOldPassWord=new TextInputLayout(AccountActivity.this);
					final TextInputEditText OldPassword=new TextInputEditText(AccountActivity.this);
					LOldPassWord.addView(OldPassword);
					LOldPassWord.setHint("旧密码");
					OldPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					TextInputLayout LPassWord=new TextInputLayout(AccountActivity.this);
					final TextInputEditText Password=new TextInputEditText(AccountActivity.this);
					LPassWord.addView(Password);
					LPassWord.setHint("密码");
					Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					TextInputLayout LPassWord2=new TextInputLayout(AccountActivity.this);
					final TextInputEditText Password2=new TextInputEditText(AccountActivity.this);
					LPassWord2.addView(Password2);
					LPassWord2.setHint("再次输入密码");
					Password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					dlgl.addView(LOldPassWord);
					dlgl.addView(LPassWord);
					dlgl.addView(LPassWord2);
					mDialog dlg=new mDialog(AccountActivity.this){
						@Override
						public void onButtonClick(int i)
						{
							if (Password.getText().toString().equals(Password2.getText().toString()))
							{
								final ProgressDialog pd=new ProgressDialog(AccountActivity.this);
								pd.setMessage("请稍后...");
								pd.setCancelable(false);
								pd.show();
								BmobUser.updateCurrentUserPassword(OldPassword.getText().toString(), Password2.getText().toString(), new UpdateListener(){
										@Override
										public void done(BmobException p1)
										{
											if(p1!=null)
											{
												mDialog dlg=new mDialog(AccountActivity.this){
													@Override
													public void onButtonClick(int i)
													{
														// TODO: Implement this method
													}
												};
												dlg.setTitle("注册发生了错误");
												dlg.setMsg("错误码:"+p1.getErrorCode()+"\n错误详情:\n"+p1.getMessage());
												dlg.setButton3("确定");
												pd.dismiss();
												dlg.show();
											}else{
												mDialog dlg=new mDialog(AccountActivity.this){
													@Override
													public void onButtonClick(int i)
													{
													}
												};
												dlg.setMsg("修改成功");
												dlg.setButton3("确定");
												dlg.setCanClose(false);
												pd.dismiss();
												dlg.show();
											}
											// TODO: Implement this method
										}
									});
							}else{
								mDialog dlg=new mDialog(AccountActivity.this){
									@Override
									public void onButtonClick(int i)
									{
										// TODO: Implement this method
									}
								};
								dlg.setMsg("两次密码不一致");
								dlg.setButton3("确定");
								dlg.show();
							}
							// TODO: Implement this method
						}
					};
					dlg.setView(dlgl);
					dlg.setButton3("修改");
					dlg.show();
					// TODO: Implement this method
				}
			});
		Button xggxq=new Button(this);
		xggxq.setText("修改个性签名");
		//xggxq.setText("")
		l.addView(xgmm);
		l.addView(xggxq);
		setContentView(l);
		getToolBarLayout().addView(info);
	}
	
}
